package models;

import java.util.Date;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.Entity;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import play.data.validation.Required;
import play.db.jpa.Model;

@Entity
/**
 * this class have all the log of the system
 * 
 * @author KimO
 */
public class Log extends Model {
	@Required
	public String ids;
	@Required
	public String types;
	@Required
	public String body;
	@Required
	public Date date;

	public void setIds(String ids) {
		this.ids = ids;
	}

	public void setTypes(String types) {
		this.types = types;
	}

	/**
	 * empty constructor
	 * 
	 * @author KimO
	 */
	public Log() {
		date = new Date();
	}

	/**
	 * constructor taking strings
	 * 
	 * @author KimO
	 * @param body
	 *            is the body of the log
	 */
	public Log(String body) {
		this.body = body;
		this.ids = "";
		this.types = "";
		date = new Date();
	}

	/**
	 *  finds the entries in the log with specifying the id and the
	 * type
	 * 
	 * @param type
	 *            the type at which the method search for
	 * @param id
	 *            the id at which the method search for
	 * @return it returns a list of logs which contain the id and the type given
	 *         or empty list if it found none
	 */
	public static List<Log> searchLog(String type, Long id) {
		// try {
		List<Log> resultLog = new ArrayList<Log>(0);
		for (int i = 0; i < Log.findAll().size(); i++) {
			Log log = ((Log) Log.findAll().get(i));
			if (log.ids.contains(id + "") && (log.types.contains(type))) {
				resultLog.add(log);
			}
		}
		if (resultLog.size() == 0)
			return null;
		return resultLog;
		// } catch (Exception e) {
		// return null;
		// }
	}

	/**
	 *  finds the entries in the log with specifying the id and the
	 * type given the initial list to search in
	 * 
	 * @param type
	 *            the type at which the method search for
	 * @param id
	 *            the id at which the method search for
	 * @param oldList
	 *            the old list where the search will be done
	 * @return it returns a list of logs which contain the id and the type given
	 *         or empty list if it found none
	 */
	public static List<Log> searchLog(List<Log> oldList, String type, Long id) {
		// try {
		List<Log> resultLog = new ArrayList<Log>(1);
		for (int i = 0; i < oldList.size(); i++) {
			Log log = ((Log) oldList.get(i));
			if (log.ids.contains(id + "") && (log.types.contains(type))) {
				resultLog.add(log);
			}
		}
		if (resultLog.size() == 0)
			return null;
		return resultLog;
		// } catch (Exception e) {
		// return null;
		// }
	}

	/**
	 *  decodes the encoded log to display it as plain text
	 * @author KimO
	 * @param log
	 *            is the log entry
	 * @return the string containing the right data
	 */
	public static String decode(Log log) {
		try {
			String temp = "";
			String result = "";
			int countId = 0;
			String[] type;
			long[] identity;
			if (!log.types.contains(",")) {
				type = new String[1];
				type[0] = log.types;
				identity = new long[1];
				identity[0] = Long.parseLong(log.ids);

			} else {
				type = log.types.split(",");
				String[] ides = log.ids.split(",");

				identity = new long[ides.length];
				for (int i = 0; i < ides.length; i++) {
					identity[i] = Long.parseLong(ides[i]);
				}
			}

			for (int i = 0; i < log.body.length(); i++) {
				if (log.body.charAt(i) == '^') {

					// /////////////////// USERS \\\\\\\\\\\\\\\\\\\\\\

					if (type[countId].toLowerCase().equals("developer")) {
						temp = "idea developer "
								+ ((IdeaDeveloper) IdeaDeveloper.find("id=?",
										identity[countId]).first()).username;
						result += "<a href=\"http://localhost:9000/userprofile/otheruserprofile?userId="
								+ identity[countId] + "\" >" + temp + "</a>";
					}

					if (type[countId].toLowerCase().equals("organizer")) {
						temp = "organizer "
								+ ((Organizer) Organizer
										.find("id = ?", identity[countId])
										.fetch().get(0)).ideaDeveloper.username;
						result += "<a href=\"http://localhost:9000/userprofile/otheruserprofile?userId="
								+ identity[countId] + "\" >" + temp + "</a>";
					}

					if (type[countId].toLowerCase().equals("organizationlead")) {
						temp = "organization lead "
								+ ((OrganizationLead) OrganizationLead
										.find("id = ?", identity[countId])
										.fetch().get(0)).ideaDeveloper.username;
						result += "<a href=\"http://localhost:9000/userprofile/otheruserprofile?userId="
								+ identity[countId] + "\" >" + temp + "</a>";
					}

					// ////// Organization , Entity , Topic , Idea \\\\\\\\\

					if (type[countId].toLowerCase().equals("organization")) {
						temp = "organization "
								+ ((Organization) Organization
										.find("id = ?", identity[countId])
										.fetch().get(0)).name;
						//
						String orgName = ((Organization) Organization
								.find("id = ?", identity[countId]).fetch()
								.get(0)).name;
						result += "<a href=\"http://localhost:9000/application/organizationview?name="
								+ orgName + "\" >" + temp + "</a>";
					}

					if (type[countId].toLowerCase().equals("entities")
							|| type[countId].toLowerCase().equals("entity")) {
						temp = "entity "
								+ ((Entities) Entities
										.find("id = ?", identity[countId])
										.fetch().get(0)).name;
						result += "<a href=\"http://localhost:9000/application/entityview?entityId="
								+ identity[countId] + "\" >" + temp + "</a>";
					}

					if (type[countId].toLowerCase().equals("topic")) {
						temp = "topic "
								+ ((Topic) Topic
										.find("id = ?", identity[countId])
										.fetch().get(0)).title;
						result += "<a href=\"http://localhost:9000/topicideacontroller/checkauthorizationtoviewtopic?topicId="
								+ identity[countId] + "\" >" + temp + "</a>";
					}

					if (type[countId].toLowerCase().equals("idea")) {
						temp = "idea "
								+ ((Topic) Topic
										.find("id = ?", identity[countId])
										.fetch().get(0)).title;
						result += "<a href=\"http://localhost:9000/topicideacontroller/checkauthorizationtoviewidea?ideaId="
								+ identity[countId] + "\" >" + temp + "</a>";
					}

					// ////////////////////////action...\\\\\\\\\\\\\\\\\\\\\\\\

					if (type[countId].toLowerCase().equals("actionitem")) {
						temp = "action item "
								+ ((ActionItem) ActionItem
										.find("id = ?", identity[countId])
										.fetch().get(0)).getId();// /ID??
						result += "<label>" + temp + "</label>";
					}

					if (type[countId].toLowerCase().equals("actionplan")) {
						temp = "action plan "
								+ ((ActionPlan) ActionPlan
										.find("id = ?", identity[countId])
										.fetch().get(0)).name;
						result += "<a href=\"http://localhost:9000/\" >" + temp
								+ "</a>";
					}

					// //////////////////comment , rate \\\\\\\\\\\\\\\\\\\\\\

					if (type[countId].toLowerCase().equals("comment")) {
						temp = "comment "
								+ ((Comment) Comment
										.find("id = ?", identity[countId])
										.fetch().get(0)).getId();// /ID??
						result += "<label>" + temp + "</label>";
					}

					if (type[countId].toLowerCase().equals("rate")) {
						temp = "rate "
								+ ((Rate) Rate
										.find("id = ?", identity[countId])
										.fetch().get(0)).getId();// /ID??
						result += "<label>" + temp + "</label>";
					}

					// //////////////////// timeline \\\\\\\\\\\\\\\\\\\\\

					if (type[countId].toLowerCase().equals("timeline")) {
						temp = "action item "
								+ ((ActionItem) ActionItem
										.find("id = ?", identity[countId])
										.fetch().get(0)).getId();// /ID??
						result += "<label>" + temp + "</label>";
					}

					// ////////////////////Request\\\\\\\\\\\\\\\\\\\\\\\\
					if (type[countId].toLowerCase().equals("request")) {
						temp = "request "
								+ ((Request) Request
										.find("id = ?", identity[countId])
										.fetch().get(0)).getId();// /ID??
						result += "<label>" + temp + "</label>";
					}

					// ////////////////////Tag\\\\\\\\\\\\\\\\\\\\\\\\

					if (type[countId].toLowerCase().equals("tag")) {
						temp = "tag "
								+ ((Tag) Tag.find("id = ?", identity[countId])
										.fetch().get(0)).getId();// /ID??
						result += "<label>" + temp + "</label>";
					}

					countId++;
				} else {
					result += log.body.charAt(i);
				}
			}
			temp.replaceFirst(temp.charAt(0) + "",
					(temp.charAt(0) + "").toUpperCase());
			result += ".";
			return result;
		} catch (Exception e) {
			return "";
		}
	}

	/**
	 * decodes the encoded log to display it as hyperlinks and only
	 * system admin can use
	 * @author KimO
	 * @param log
	 *            is the log entry
	 * @return the string containing the right data
	 */
	public static String decodeAdmin(Log log) {
		try {
			String temp = "";
			String result = "";
			int countId = 0;
			String[] type;
			long[] identity;
			if (!log.types.contains(",")) {
				type = new String[1];
				type[0] = log.types;
				identity = new long[1];
				identity[0] = Long.parseLong(log.ids);

			} else {
				type = log.types.split(",");
				String[] ides = log.ids.split(",");

				identity = new long[ides.length];
				for (int i = 0; i < ides.length; i++) {
					identity[i] = Long.parseLong(ides[i]);
				}
			}

			for (int i = 0; i < log.body.length(); i++) {
				if (log.body.charAt(i) == '^') {

					// /////////////////// USERS \\\\\\\\\\\\\\\\\\\\\\

					if (type[countId].toLowerCase().equals("developer")) {
						temp = "idea developer "
								+ ((IdeaDeveloper) IdeaDeveloper.find("id=?",
										identity[countId]).first()).username;
						result += "<a href=\"http://localhost:9000/ideadevelopers/show?id="
								+ identity[countId] + "\" >" + temp + "</a>";
					}

					if (type[countId].toLowerCase().equals("organizer")) {
						temp = "organizer "
								+ ((Organizer) Organizer
										.find("id = ?", identity[countId])
										.fetch().get(0)).ideaDeveloper.username;
						result += "<a href=\"http://localhost:9000/organizers/show?id="
								+ identity[countId] + "\" >" + temp + "</a>";
					}

					if (type[countId].toLowerCase().equals("organizationlead")) {
						temp = "organization lead "
								+ ((OrganizationLead) OrganizationLead
										.find("id = ?", identity[countId])
										.fetch().get(0)).ideaDeveloper.username;
						result += "<a href=\"http://localhost:9000/organizationleads/show?id="
								+ identity[countId] + "\" >" + temp + "</a>";
					}

					// ////// Organization , Entity , Topic , Idea \\\\\\\\\

					if (type[countId].toLowerCase().equals("organization")) {
						temp = "organization "
								+ ((Organization) Organization
										.find("id = ?", identity[countId])
										.fetch().get(0)).name;
						result += "<a href=\"http://localhost:9000/organizations/show?id="
								+ identity[countId] + "\" >" + temp + "</a>";
					}

					if (type[countId].toLowerCase().equals("entities")
							|| type[countId].toLowerCase().equals("entity")) {
						temp = "entity "
								+ ((Entities) Entities
										.find("id = ?", identity[countId])
										.fetch().get(0)).name;
						result += "<a href=\"http://localhost:9000/entitiess/show?id="
								+ identity[countId] + "\" >" + temp + "</a>";
					}

					if (type[countId].toLowerCase().equals("topic")) {
						temp = "topic "
								+ ((Topic) Topic
										.find("id = ?", identity[countId])
										.fetch().get(0)).title;
						result += "<a href=\"http://localhost:9000/topics/show?id="
								+ identity[countId] + "\" >" + temp + "</a>";
					}

					if (type[countId].toLowerCase().equals("idea")) {
						temp = "idea "
								+ ((Topic) Topic
										.find("id = ?", identity[countId])
										.fetch().get(0)).title;
						result += "<a href=\"http://localhost:9000/ideas/show?id="
								+ identity[countId] + "\" >" + temp + "</a>";
					}

					// ////////////////////////action...\\\\\\\\\\\\\\\\\\\\\\\\

					if (type[countId].toLowerCase().equals("actionitem")) {
						temp = "action item "
								+ ((ActionItem) ActionItem
										.find("id = ?", identity[countId])
										.fetch().get(0)).getId();// /ID??
						result += "<a href=\"http://localhost:9000/actionitems/show?id="
								+ identity[countId] + "\" >" + temp + "</a>";
					}

					if (type[countId].toLowerCase().equals("actionplan")) {
						temp = "action plan "
								+ ((ActionPlan) ActionPlan
										.find("id = ?", identity[countId])
										.fetch().get(0)).name;
						result += "<a href=\"http://localhost:9000/actionplans/show?id="
								+ identity[countId] + "\" >" + temp + "</a>";
					}

					// //////////////////comment , rate \\\\\\\\\\\\\\\\\\\\\\

					if (type[countId].toLowerCase().equals("comment")) {
						temp = "comment "
								+ ((Comment) Comment
										.find("id = ?", identity[countId])
										.fetch().get(0)).getId();// /ID??
						result += "<label>" + temp + "</label>";
					}

					if (type[countId].toLowerCase().equals("rate")) {
						temp = "rate "
								+ ((Rate) Rate
										.find("id = ?", identity[countId])
										.fetch().get(0)).getId();// /ID??
						result += "<label>" + temp + "</label>";
					}

					// //////////////////// timeline \\\\\\\\\\\\\\\\\\\\\

					if (type[countId].toLowerCase().equals("timeline")) {
						temp = "action item "
								+ ((ActionItem) ActionItem
										.find("id = ?", identity[countId])
										.fetch().get(0)).getId();// /ID??
						result += "<label>" + temp + "</label>";
					}
					// ////////////////////Request\\\\\\\\\\\\\\\\\\\\\\\\

					if (type[countId].toLowerCase().equals("request")) {
						temp = "request "
								+ ((Request) Request
										.find("id = ?", identity[countId])
										.fetch().get(0)).getId();// /ID??
						result += "<label>" + temp + "</label>";
					}

					// ////////////////////Tag\\\\\\\\\\\\\\\\\\\\\\\\

					if (type[countId].toLowerCase().equals("tag")) {
						temp = ((Tag) Tag.find("id = ?", identity[countId])
								.fetch().get(0)).name;// /ID??
						result += "<a href=\"http://localhost:9000/TagPage/TagView?name="
								+ temp + "\" >" + temp + "</a>";
					}

					countId++;
				} else {
					result += log.body.charAt(i);
				}
			}
			temp.replaceFirst(temp.charAt(0) + "",
					(temp.charAt(0) + "").toUpperCase());
			result += ".";
			return result;
		} catch (Exception e) {
			return "";
		}
	}
}
// http://localhost:9000/ideadevelopers/show?id=